package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.utils.IPUtil;
import cn.chinaflame.pagodas.vo.LightVO;
import cn.chinaflame.pagodas.vo.ResponseFormat;
import cn.chinaflame.pagodas.web.webservice.LightniWebService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 连接硬件相关接口（暂未使用）
 *
 * @author admin
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/wrmnl/")
public class WrmNlAPI {
	   private static final Logger log = LoggerFactory.getLogger(WrmNlAPI.class);
    /**
     * 寻订
     *
     * @param request
     * @param lightno 寻灯/点灯对象
     * @return
     */
    @PostMapping(value = "lightSearch")
    @ApiDoc(Result.class)
    public Object lightSearch(HttpServletRequest request, @RequestBody LightVO lightvo) {
    	log.info("lightvo:{}",lightvo);
        String ip = "";
        try {
            ip = IPUtil.getIpAddr(request);
            Result checkresult = paramCheck(ip, lightvo.getLightno());
            if (checkresult != null && StringUtils.isNotEmpty(checkresult.getCode()) && checkresult.getCode().toLowerCase().equals(CodeEnum.FAIL.getCode().toLowerCase())) {
                return checkresult;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = LightniWebService.invokeLightSearch(lightvo.getLightno(), null);
        ResponseFormat responseformat = JSONObject.parseObject(result, ResponseFormat.class);
        switch (responseformat.getResult()) {
            case 0:
                return Result.create(CodeEnum.FAIL.getCode(), responseformat.getMessage());
            case 1:
                return Result.create(CodeEnum.SUCCESS.getCode(), responseformat.getMessage());
            default:
                break;
        }
        return null;
    }

    /**
     * 安灯
     *
     * @param request
     * @param lightno
     * @param obj
     * @return
     */
    @PostMapping(value = "lightSend")
    @ApiDoc(Result.class)
    public Object lightSend(HttpServletRequest request, @RequestBody LightVO lightvo) {//String lightno, String obj
        String ip = "";
        try {
            ip = IPUtil.getIpAddr(request);
            Result checkresult = paramCheck(ip, lightvo.getLightno());
            if (checkresult != null && StringUtils.isNotEmpty(checkresult.getCode()) && checkresult.getCode().toLowerCase().equals(CodeEnum.FAIL.getCode().toLowerCase())) {
                return checkresult;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = LightniWebService.invokeLightSend(lightvo.getLightno(), lightvo.getObj(), ip);
        ResponseFormat responseformat = JSONObject.parseObject(result, ResponseFormat.class);
        switch (responseformat.getResult()) {
            case 0:
                return Result.create(CodeEnum.FAIL.getCode(), responseformat.getMessage());
            case 1:
                return Result.create(CodeEnum.SUCCESS.getCode(), responseformat.getMessage());
            default:
                break;
        }
        return null;
    }

    /**
     * 清灯
     *
     * @param request
     * @param lightno
     * @return
     */
    @PostMapping(value = "lightClear")
    @ApiDoc(Result.class)
    public Object lightClear(HttpServletRequest request, @RequestBody LightVO lightvo) {
        String ip = "";
        try {
            ip = IPUtil.getIpAddr(request);
            Result checkresult = paramCheck(ip, lightvo.getLightno());
            if (checkresult != null && StringUtils.isNotEmpty(checkresult.getCode()) && checkresult.getCode().toLowerCase().equals(CodeEnum.FAIL.getCode().toLowerCase())) {
                return checkresult;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = LightniWebService.invokeLightClear(lightvo.getLightno(), ip);
        ResponseFormat responseformat = JSONObject.parseObject(result, ResponseFormat.class);
        switch (responseformat.getResult()) {
            case 0:
                return Result.create(CodeEnum.FAIL.getCode(), responseformat.getMessage());
            case 1:
                return Result.create(CodeEnum.SUCCESS.getCode(), responseformat.getMessage());
            default:
                break;
        }
        return null;
    }

    /**
     * 检查灯位
     *
     * @param request
     * @param lightno
     * @return
     */
    @PostMapping(value = "lightCheck")
    @ApiDoc(Result.class)
    public Object lightCheck(HttpServletRequest request, @RequestBody LightVO lightvo) {
        String ip = "";
        try {
            ip = IPUtil.getIpAddr(request);
            Result checkresult = paramCheck(ip, lightvo.getLightno());
            if (checkresult != null && StringUtils.isNotEmpty(checkresult.getCode()) && checkresult.getCode().toLowerCase().equals(CodeEnum.FAIL.getCode().toLowerCase())) {
                return checkresult;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = LightniWebService.invokeLightCheck(lightvo.getLightno(), ip);
        ResponseFormat responseformat = JSONObject.parseObject(result, ResponseFormat.class);
        switch (responseformat.getResult()) {
            case 0:
                return Result.create(CodeEnum.FAIL.getCode(), responseformat.getMessage());
            case 1:
                return Result.create(CodeEnum.SUCCESS.getCode(), responseformat.getMessage());
            default:
                break;
        }
        return null;
    }

    /**
     * 发送配置
     *
     * @param request
     * @param seateinfo
     * @return
     */
    @PostMapping(value = "lightConfigure")
    @ApiDoc(Result.class)
    public Object LightConfigure(HttpServletRequest request, @RequestBody LightVO lightvo) {
        String ip = "";
        try {
            ip = IPUtil.getIpAddr(request);
            Result checkresult = paramCheck(ip, lightvo.getLightno());
            if (checkresult != null && StringUtils.isNotEmpty(checkresult.getCode()) && checkresult.getCode().toLowerCase().equals(CodeEnum.FAIL.getCode().toLowerCase())) {
                return checkresult;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = LightniWebService.invokeLightConfigure(lightvo.getLightno(), ip);
        ResponseFormat responseformat = JSONObject.parseObject(result, ResponseFormat.class);
        switch (responseformat.getResult()) {
            case 0:
                return Result.create(CodeEnum.FAIL.getCode(), responseformat.getMessage());
            case 1:
                return Result.create(CodeEnum.SUCCESS.getCode(), responseformat.getMessage());
            default:
                break;
        }
        return null;
    }


    private Result paramCheck(String ipaddr, String lightno) {
        //try {
			/*if(!IPUtil.IsValidIp(ipaddr)) {
				return Result.create("ERROR", "请输入正确的IP地址");
			};*/
		/*} catch (IOException e) {
			e.printStackTrace();
			return Result.create("ERROR","连接指令服务失败，请检查网络及IP是否正常");
		}*/
        if (StringUtils.isEmpty(lightno) || StringUtils.isBlank(lightno)) {
            return Result.create(CodeEnum.FAIL.getCode(), "请输入手机号或寻灯编号");
        }
        return null;
    }
}
